<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>后台登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }

        body {
            background-color: #f5f7fa;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .login-container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 100%;
            max-width: 400px;
        }

        .login-title {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
            font-size: 24px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: bold;
        }

        .form-group input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        .form-group input:focus {
            border-color: #4a90e2;
            outline: none;
        }

        .login-btn {
            width: 100%;
            padding: 12px;
            background-color: #4a90e2;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .login-btn:hover {
            background-color: #357ac0;
        }

        .error-message {
            color: #e74c3c;
            text-align: center;
            margin-top: 15px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2 class="login-title">后台管理系统</h2>
        <form id="login-form">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" placeholder="请输入用户名" required>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码" required>
            </div>
            <button type="submit" class="login-btn">登录</button>
            <div class="error-message" id="error-message">用户名或密码错误</div>
        </form>
    </div>

    <script>
        document.getElementById('login-form').addEventListener('submit', function(e) {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const errorMessage = document.getElementById('error-message');
            const loginBtn = document.querySelector('.login-btn');

            // 禁用按钮，防止重复提交
            loginBtn.disabled = true;
            loginBtn.textContent = '登录中...';

            // 调用后端登录API
            fetch('/token', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: new URLSearchParams({
                    'username': username,
                    'password': password
                })
            })
            .then(response => {
                loginBtn.disabled = false;
                loginBtn.textContent = '登录';
                
                if (!response.ok) {
                    throw new Error('用户名或密码错误');
                }
                return response.json();
            })
            .then(data => {
                // 登录成功，存储登录状态、token和用户名
                localStorage.setItem('isLoggedIn', 'true');
                localStorage.setItem('token', data.access_token);
                localStorage.setItem('username', username);
                
                console.log('登录成功，token:', data.access_token);
                window.location.href = '/static/admin_dashboard.html';
            })
            .catch(error => {
                // 登录失败，显示错误信息
                errorMessage.textContent = error.message;
                errorMessage.style.display = 'block';
                setTimeout(() => {
                    errorMessage.style.display = 'none';
                }, 3000);
            });
        });

        // 检查是否已登录，如果已登录则直接跳转到后台管理页面
        if (localStorage.getItem('isLoggedIn') === 'true') {
            window.location.href = '/static/admin_dashboard.html';
        }
    </script>
</body>
</html>